package com.longcai.service.impl.sys;

import com.longcai.entity.auth.SysShopsPermission;
import com.longcai.entity.auth.SysShopsUser;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.sys.SysUser;
import com.longcai.mapper.SysShopsPermissionMapper;
import com.longcai.service.clyjuser.ClyjUserService;
import com.longcai.service.sys.SysShopsUserService;
import com.longcai.util.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 * @Title: MyUserDetailsServiceImpl
 * @ProjectName spring-cloud-media
 * @Description: TODO
 * @date 2019/2/2111:58
 */
@Service("UserDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysShopsUserService sysUserService;
    @Autowired
    private SysShopsPermissionMapper SysShopsPermissionMapper;
    @Autowired
    private  ClyjUserService clyjUserService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ClyjUser clyjUser = this.clyjUserService.getUser(username);
        if (clyjUser == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }

        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(clyjUser, loginUser);

        List<SysShopsPermission> permissions = this.SysShopsPermissionMapper.listByUserId(clyjUser.getUserId());
        loginUser.setPermissions(permissions);

        return loginUser;
    }
}
